package com.example.javanewwork.controller;


import com.example.javanewwork.entity.Result;
import com.example.javanewwork.mapper.UserMapper;
import com.example.javanewwork.service.Userservice;
import com.example.javanewwork.utils.JwtUtil;
import com.example.javanewwork.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.javanewwork.entity.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class Usercontroller {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private Userservice userservice;


    @GetMapping("/info")
    public User info(@RequestHeader(name = "Token") String token){
        System.out.println(token);
        //解析token
        Map<String,Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");
        System.out.println(username);
        User user = userservice.findByUserName(username);
        return user;
    }


    @PostMapping("/register")
    public Result<String> register(@RequestBody User user){
//        if(username != null && username.length()>=6 && username.length()<=16
//        && password!= null&& password.length()>=6 && password.length()<=12)
//        {
        //查询
        String username=user.getUsername();
        String password= user.getPassword();
        User u = userservice.findByUserName(username);
        if(u == null)
        {
            //注册
            userservice.register(username,password);
            User loginUser = userservice.findByUserName(username);
            Map<String,Object> claims  = new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtil.genToken(claims);

            return Result.success(token);
        }else {
            return Result.error("用户已经注册了");
        }


        //注册
    }

    @PostMapping("/edit")
    public boolean edituserinfo(@RequestBody User user,@RequestHeader(name = "Token") String token){
        System.out.println(token);
        Map<String,Object> map = JwtUtil.parseToken(token);
        String name = (String) map.get("username");
        user.setUsername(name);
        System.out.println(user);
        userservice.Update(user);
        return true;
    }
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user){
        //根据用户名查询用户
        String username=user.getUsername();
        String password= user.getPassword();
        User loginUser = userservice.findByUserName(username);
//        System.out.println(username);
//        System.out.println(password);
        //判断用户是否存在
        if(loginUser == null)
        {
            return Result.error("用户名错误");

        }
        //密码是否正确
        if(password.equals(loginUser.getPassword())){

            //登录成功
            Map<String,Object> claims  = new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtil.genToken(claims);

            return Result.success(token);
        }
        return Result.error("密码错误");
    }


    @GetMapping("/")
    public List<User> index()
    {
        return userMapper.findAll();
    }
    @PostMapping()
    public Integer save(@RequestBody User user){
         return userservice.save(user);
    }

    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize
                                        ,@RequestParam String username){
        pageNum = (pageNum-1)*pageSize;
        username = "%"+username+"%";
        Integer total = userMapper.selectTotal(username);
        List<User> data = userMapper.selectPage(pageNum,pageSize,username);
        Map<String,Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }
}
